'use strict';

angular.module('snPaas')
.directive('spSlider',['baseUrl','$filter',function(baseUrl,$filter){
    return {
      restrict:'A',
      priority:-1,
      templateUrl :baseUrl.static+'template/directive/spSlider.html',
      replace:true,
      scope:{
         spValue: '=',
         spInterval: '=',
         spMin: '=',
         spMax: '='
      },
      link:function postLink(scope,iElement,iAttrs){
        var filterFn = function(v){
          return v;
        };
        if(iAttrs.spFilter){
          scope.filter = iAttrs.spFilter;
          filterFn = function(v){
            return $filter(iAttrs.spFilter)(v);
          };
        }
        var originX = 0,
            width = 0,
            widthNow = 0,
            stepMax = 0,
            valueNow = 0,
            wn = 0,
            init = function(){
              scope.baseUrl = baseUrl.static;
              scope.val = scope.spValue;
              if(!scope.spInterval){
                scope.spInterval = 1;
              }
              if(!scope.spMin){
                scope.spMin = 0;
              }
              if(!scope.spMax){
                return false;
              } 
              if(!scope.spValue || scope.spValue<scope.spMin){
                scope.spValue = scope.spMin;
              }
              iElement.find('.number').html(filterFn(scope.spValue));
              width = iElement.find('.sp-slider-main').width();
              stepMax = Math.ceil((scope.spMax-scope.spMin)/scope.spInterval);
              widthNow = ((scope.spValue - scope.spMin)/(scope.spMax - scope.spMin))*width;
              iElement.find('.bar').width(widthNow);
              return true;

            },

            moveFn = function(e){
              var x = e.clientX;
              var vexX = x - originX;
              wn = widthNow+vexX;
              if(wn>width){
                wn = width;
              }
              else if(wn<0){
                wn = 0;
              }
              iElement.find('.bar').width(wn);
              var v = Math.ceil((wn/width)*stepMax-0.5);
              iElement.find('.number').html(filterFn(v*scope.spInterval + scope.spMin));
              valueNow = v*scope.spInterval + scope.spMin;
            },
            upFn = function(e){
              $('body').off('mousemove',moveFn);
              $('body').off('mouseup',upFn);
              scope.spValue = valueNow;
              widthNow = wn;
            }
        ;
        if(!init()){
          return;
        }
        scope.$watch('spValue',function(n,o){
          if(n!=o){
            init();
          }
        });
        iElement.find('.dot').mousedown(function(e){
          if(e.target.tagName =='DIV'){
            originX = e.clientX;
            $('body').on('mousemove',moveFn);
            $('body').on('mouseup',upFn);
          }
          
        })
      }
   }
}])
.directive('spMultiSlider',['baseUrl','$filter',function(baseUrl,$filter){
    return {
      restrict:'A',
      priority:-1,
      templateUrl :baseUrl.static+'template/directive/spMultiSlider.html',
      replace:true,
      scope:{
         spValue: '=',
         spInterval: '=',
         spMin: '=',
         spMax: '='
      },
      link:function postLink(scope,iElement,iAttrs){
        var filterFn = function(v){
          return v;
        };
        if(iAttrs.spFilter){
          scope.filter = iAttrs.spFilter;
          filterFn = function(v){
            return $filter(iAttrs.spFilter)(v);
          };
        }
        var originX = 0,
            width = 0,
            widthNow = [0,0],
            stepMax = 0,
            valueNow = 0,
            wn = 0,
            barIndex = 0,
            init = function(){
              scope.baseUrl = baseUrl.static;
              if(!scope.spValue){
                scope.spValue = [0,0];
              }
              scope.val = scope.spValue; 
              if(!scope.spMin){
                scope.spMin = 0;
              }
              if(!scope.spMax){
                return false;
              }
              if(!scope.spValue[0] || scope.spValue[0]<scope.spMin){
                scope.spValue[0] = scope.spMin;
              }
              if(!scope.spValue[1] || scope.spValue[1]>scope.spMax){
                scope.spValue[1] = scope.spMax;
              }
              if(scope.spValue[0]>scope.spValue[1]){
                var tmp = scope.spValue[1];
                scope.spValue[1] = scope.spValue[0];
                scope.spValue[0] = tmp;
              }
              if(!scope.spInterval){
                scope.spInterval = 1;
              }
              
              iElement.find('.bar1 .number').html(filterFn(scope.spValue[1]));
              iElement.find('.bar0 .number').html(filterFn(scope.spValue[0]));

              width = parseInt(iElement.find('.sp-slider-main').css('width'));
              stepMax = Math.ceil((scope.spMax-scope.spMin)/scope.spInterval);
              widthNow[0] = ((scope.spValue[0] - scope.spMin)/(scope.spMax - scope.spMin))*width;
              iElement.find('.bar0').width(widthNow[0]);
              widthNow[1] = ((scope.spValue[1] - scope.spMin)/(scope.spMax - scope.spMin))*width;
              iElement.find('.bar1').width(widthNow[1]);
              return true;

            },

            moveFn = function(e){
              var x = e.clientX;
              var vexX = x - originX;
              wn = widthNow[barIndex]+vexX;
              if(barIndex ==0){
                if(wn>widthNow[1]){
                  wn = widthNow[1];
                }
                else if(wn<0){
                  wn = 0;
                }
              }
              else{
                if(wn>width){
                  wn = width;
                }
                else if(wn<widthNow[0]){
                  wn = widthNow[0];
                }
              }
              
              iElement.find('.bar'+barIndex).width(wn);
              var v = Math.ceil((wn/width)*stepMax-0.5);
              iElement.find('.bar'+barIndex+' .number').html(filterFn(v*scope.spInterval + scope.spMin));
              valueNow = v*scope.spInterval + scope.spMin;
            },
            upFn = function(e){
              $('body').off('mousemove',moveFn);
              $('body').off('mouseup',upFn);
              scope.spValue[barIndex] = valueNow;
              widthNow[barIndex] = wn;
            }
        ;
        if(!init()){
          return;
        }
        scope.$watch('spValue[0]',function(n,o){
          if(n!=o){
            init();
          }
        });
        scope.$watch('spValue[1]',function(n,o){
          if(n!=o){
            init();
          }
        });
        iElement.find('.dot').mousedown(function(e){
          if(e.target.tagName =='DIV'){
            barIndex = parseInt($(this).attr('data-index'));
            originX = e.clientX;
            $('body').on('mousemove',moveFn);
            $('body').on('mouseup',upFn);
          }
          
        })
      }
   }

}])


;